package com.sinodata.bsm.cicp.finder.hp;

/**
 * HP操作系统 FS的发现程序
 * 实现方式:bdf
 *
 # bdf
 Filesystem          kbytes    used   avail %used Mounted on
 /dev/vg00/lvol3    1048576  489428  524668   48% /
 /dev/vg00/lvol1     295024   34104  231416   13% /stand
 /dev/vg00/lvol9    1048576  960037   86349   92% /var
 /dev/vg00/lvol8     675840  604086   67323   90% /usr
 /dev/vg00/lvol5     204800    9072  184157    5% /tmp
 /dev/vg00/lvol4    6880524 5245789  946682   85% /oracle
 /dev/vg00/lvol7    3264512 2186892 1015760   68% /opt
 /dev/vg00/lvol6    2097152 1616929  450349   78% /home
 #
 * @author zhaoch
 * @version 1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class FS extends HPFinder {
    private static final Logger logger = Logger.getLogger(FS.class);
    private static final String SHELL = "bdf | awk '{if(NF==1) printf(\"%s\", $0); else print $0;}' | tr -d ':'|sed s/\\\\./_/g";

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] fsRes = null;
        BufferedReader reader = null;
        BufferedReader strReader = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            String str = null;
            Pattern ptnAll = Pattern.compile("(/\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(/.*)");
            Pattern ptnName = Pattern.compile("^(/\\S+)");
            Pattern ptnValue = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(/.*)");
            StringBuffer strBuffer = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                str = str.trim();
                Matcher matAll = ptnAll.matcher(str);
                Matcher matName = ptnName.matcher(str);
                if (matAll.find()) {
                    strBuffer.append(str);
                    strBuffer.append("\n");
                } else if (matName.find()) {
                    String strtmp = reader.readLine();
                    Matcher matValue = ptnValue.matcher(strtmp);
                    if (strtmp != null && matValue.find()) {
                        strBuffer.append(str);
                        strBuffer.append("  ");
                        strBuffer.append(strtmp);
                        strBuffer.append("\n");
                    }
                }
            }
            strReader = new BufferedReader(new StringReader(strBuffer.toString()));
            while ((str = strReader.readLine()) != null) {
                str = str.trim();
                Matcher matcher = ptnAll.matcher(str);
                if (matcher.find()) {
                    String vgName = matcher.group(1); //所属逻辑卷设备
                    String mount = matcher.group(6); //挂载点
                    fsRes = new String[5];
                    list.add(fsRes);
                    fsRes[0] = mount;
                    fsRes[1] = mount;
                    fsRes[2] = getHostName(ip) + " FS " + mount;
                    fsRes[3] = mount; //所属逻辑卷设备
                    fsRes[4] = vgName; //挂载点
                }
            }
            return list;
        } catch (Throwable t) {
            logger.error("failed to find hp FS", t);
            return null;
        } finally {
            if (strReader != null) {
                try {
                    strReader.close();
                } catch (IOException ex) {
                    logger.error("failed to close reader", ex);
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
    }
}
